import { app, BrowserWindow } from 'electron';

import Configuration from './config';
import RegisterEvents from './events';
import LoadMenu from './menu';
import ThreadController from './scancontrol';
import RegisterShortcuts from './shortcut';

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
    global.__static = require('path')
        .join(__dirname, '/static')
        .replace(/\\/g, '\\\\');
}

let mainWindow;
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html`;

app.setName('Fancy Slide');

Configuration.initialize();

function createWindow() {
    LoadMenu();
    RegisterShortcuts();
    /**
   * Initial window options
   */
    mainWindow = new BrowserWindow({
        height: 563,
        useContentSize: true,
        width: 1000,
        // kiosk: true,
        frame: false,
        titleBarStyle: 'hidden',
        webPreferences: {
            webSecurity: false
        }
    });

    mainWindow.loadURL(winURL);

    mainWindow.on('closed', () => {
        mainWindow = null;
    });

    mainWindow.on('resize', () => {
        mainWindow.webContents.send('window-size-changed', mainWindow.getContentSize());
    });

    mainWindow.on('enter-full-screen', () => {
        mainWindow.webContents.send('enter-full-screen');
    });

    mainWindow.on('leave-full-screen', () => {
        mainWindow.webContents.send('leave-full-screen');
    });

    mainWindow.webContents.on('did-finish-load', () => {
        mainWindow.webContents.send('window-size-changed', mainWindow.getContentSize());
        mainWindow.webContents.send('set-configuration', Configuration.config);
    });

    // mainWindow.maximize();
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow();
    }
});

app.on('quit', () => {
    ThreadController.exitThread();
});

RegisterEvents();

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
